package com.larry.mr.inputformat;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.ByteWritable;
import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

/**
 * @ProjectName map-reduce
 * @Author larry
 * @Date 20:15 2022/3/30
 * @Description TODO
 **/
public class WholeRecordReader extends RecordReader<Text, BytesWritable> {

    private FileSplit split;
    private Configuration configuration;

    private BytesWritable value = new BytesWritable();
    private Text key = new Text();
    private boolean isProgress = true;

    @Override
    public void initialize(InputSplit inputSplit, TaskAttemptContext taskAttemptContext) throws IOException, InterruptedException {

        this.split = (FileSplit) inputSplit;
        configuration = taskAttemptContext.getConfiguration();
    }

    /**
     * 核心方法
     * @return
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean nextKeyValue() throws IOException, InterruptedException {

        if (isProgress){
            //定义缓冲区
            byte[] bytes = new byte[(int) split.getLength()];
            //
            FileSystem fs = null;
            FSDataInputStream fis = null;

            //获取文件系统
            Path path = split.getPath();
            fs = path.getFileSystem(configuration);

            //读取数据
            fis = fs.open(path);

            //读取文件内容
            IOUtils.readFully(fis,bytes,0,bytes.length);

            //输出文件内容
            value.set(bytes,0,bytes.length);

            //获取文件路径及名称
            String name = split.getPath().toString();

            //设置输出key
            key.set(name);

            isProgress = false;

            return true;
        }
        return false;
    }

    @Override
    public Text getCurrentKey() throws IOException, InterruptedException {
        return key;
    }

    @Override
    public BytesWritable getCurrentValue() throws IOException, InterruptedException {
        return value;
    }

    @Override
    public float getProgress() throws IOException, InterruptedException {
        return 0;
    }

    @Override
    public void close() throws IOException {

    }
}
